<?php

/**
 * Controller_memberac
 * 团支部活动安排
 * 
 */

class Controller_memberac extends MyApp {

	public function __construct()
	{

		parent::__construct();
        $this->model->semester = YOU::loadModel('semester');
		$this->model->syllabus = YOU::loadModel('syllabus');
		$this->model->grade    = YOU::loadModel('grade');
		$this->model->class    = YOU::loadModel('class');
		$this->model->subject  = YOU::loadModel('subject');
		$this->model->staff    = YOU::loadModel('staff');
		$this->model->excel    = YOU::loadModel('excel');
		$this->model->student  = YOU::loadModel('student');
	}
	
	/** 首页 **/
	public function actionIndex()
	{

	}

	/** 活动安排列表 **/
	public function actionList ()
	{
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp  = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_memberac`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$memberac_lists = $this->db->fetchAll("SELECT * FROM `tbl_memberac` ORDER BY memberac_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		//导出excel
		$re=$this->get->output ;
		if($re == "excel"){
			include_once("lib/phpexcel/PHPExcel.php");
			include_once("lib/phpexcel/PHPExcel/Writer/Excel2007.php");

			// 输出excel
			$objPHPExcel = new PHPExcel();

			// 输出表头
			$a = 0;
			foreach ($record_lists as $key => $value){
				$objPHPExcel->getActiveSheet()->setCellValue($this->letter_ini[$a] . 1, $value['title']);
				$a++;
			}

			// 输出数据 - 从第2行开始
			$i =2;
			foreach ($staffnew_lists as $key => $value){
				$a = 0;
				foreach ($record_lists as $k => $v){
					$objPHPExcel->getActiveSheet()->setCellValue($this->letter_ini[$a] . $i, (string)$value[$v['name']]);
					$a++;
				}
				$i++;
			}

			$objPHPExcel->createSheet();
			$fname = date("YmdHis").".xls";

			//保存excel—2007格式
			//$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
			//$objWriter->save("public/excel/xxx.xlsx");

			//直接输出到浏览器
			$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
			header("Pragma: public");
			header("Expires: 0");
			header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
			header("Content-Type:application/force-download");
			header("Content-Type:application/vnd.ms-execl");
			header("Content-Type:application/octet-stream");
			header("Content-Type:application/download");;
			header("Content-Disposition:attachment;filename={$fname}");
			header("Content-Transfer-Encoding:binary");

			$objWriter->save('php://output');

			exit;
		}

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("memberac_lists", $memberac_lists);
		$this->tpl->display('memberac/memberac.list.tpl.php');

	}
	/** 活动安排添加 **/
	public function actionAdd()
	{
		$memberac_lists = $this->db->fetchAll("SELECT * FROM `tbl_memberac`");

		if ($_POST){
			$memberac_title        = $this->post->memberac_title;
			$memberac_user         = $this->post->memberac_user;
			$memberac_con          = $this->post->memberac_con;
			$memberac_date         = $this->post->memberac_date;
			if (empty($memberac_title) || empty($memberac_con)) {
				Helper_Client::location("所有栏目必须填满", "index.php?a=memberac.add&memberac_id={$memberac_id}");
			}

			// 写入数据库
			$info = array(
			"memberac_title"        => $memberac_title,
			"memberac_user"         => $memberac_user,
			"memberac_con"          => $memberac_con,
			"memberac_date"         => $memberac_date,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_memberac", $info);
			$memberac_id = $this->db->insert_id();
			//上传图片

			if (!empty($_FILES['memberac_pic']['tmp_name'])) {
				$up_name ="{$memberac_id}.png";
				$up_path = UPLOAD_PATH."/teacher_in/ ".$up_name;
				copy($_FILES['memberac_pic']['tmp_name'], $up_path);
				$info = array(
				"memberac_pic" => $up_path,
				);
				$this->db->do_update("tbl_memberac", $info, "memberac_id='{$memberac_id}'");
				if (!empty($_FILES['memberac_file']['tmp_name'])){
					$up_name = "{$memberac_id}.xls";
					$up_path = UPLOAD_PATH."/teacher_in/{$up_name}";
					copy($_FILES['memberac_file']['tmp_name'], $up_path);

					$info = array(
					"memberac_file" => $up_name,
					);
					$this->db->do_update("tbl_memberac", $info, "memberac_id='{$memberac_id}'");
				}
			}


			// 跳转
			Helper_Client::location("添加成功", "index.php?a=memberac.list ");
		}
		$type_lists=$this->db->fetchAll("SELECT * FROM `tbl_mtype`");
		$this->tpl->assign("type_lists", $type_lists);
		$this->tpl->assign("memberac_lists", $memberac_lists);
		$this->tpl->display('memberac/memberac.add.tpl.php');
	}
	/** 活动安排更改 **/
	public function actionUpdate()
	{
		$memberac_id   = (int)$this->get->memberac_id;
		$memberac_data = $this->db->fetchRow("SELECT * FROM `tbl_memberac` WHERE `memberac_id`='{$memberac_id}'");
		if (empty($memberac_data)){
			Helper_Client::alert("活动不存在！");
		}

		if ($_POST){
			$memberac_title        = $this->post->memberac_title;
			$memberac_user         = $this->post->memberac_user;
			$memberac_con          = $this->post->memberac_con;
			$memberac_date         = $this->post->memberac_date;
			if (empty($memberac_title) || empty($memberac_con)) {
				Helper_Client::location("所有栏目必须填满", "index.php?a=memberac.update&memberac_id={$memberac_id}");
			}

			// 写入数据库
			$info = array(
			"memberac_title"        => $memberac_title,
			"memberac_user"         => $memberac_user,
			"memberac_con"          => $memberac_con,
			"memberac_date"         => $memberac_date,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_update("tbl_memberac", $info, "memberac_id='{$memberac_id}'");
			//上传图片

			if (!empty($_FILES['memberac_pic']['tmp_name'])) {
				$up_name ="{$memberac_id}.png";
				$up_path = UPLOAD_PATH."/ teacher_in/ {$up_name}";
				copy($_FILES['memberac_pic']['tmp_name'], $up_path);
				$info = array(
				"memberac_pic" => $up_name,
				);
				$this->db->do_update("tbl_memberac", $info, "memberac_id='{$memberac_id}'");
				if (!empty($_FILES['memberac_file']['tmp_name'])){

					$up_name = "{$memberac_id}.xls";
					$up_path  = UPLOAD_PATH."/ teacher_in"."/{$up_name}";
					copy($_FILES['memberac_file']['tmp_name'], $up_path);

					$info = array(
					"memberac_file" => $up_name,
					);
					$this->db->do_update("tbl_memberac", $info, "memberac_id='{$memberac_id}'");
				}

			}
			// 跳转
			Helper_Client::location("修改成功", "index.php?a=memberac.list");
		}


		$this->tpl->assign("memberac_data", $memberac_data);
		$this->tpl->display('memberac/memberac.update.tpl.php');
	}
	/** 删除活动 **/
	public function actionDel()
	{
		$memberac_id = $this->get->memberac_id;
		$this->db->query("DELETE  FROM `tbl_memberac` WHERE memberac_id='{$memberac_id}'");

		// 跳转
		Helper_Client::location("删除活动安排成功", "index.php?a=memberac.list");
	}
	public function actionInfo(){
		$memberac_id   = (int)$this->get->memberac_id;
		$memberac_data = $this->db->fetchROW("SELECT * FROM `tbl_memberac` WHERE `memberac_id`='{$memberac_id}'");

		if (empty($memberac_data)){
			Helper_Client::alert("活动不存在！");
		}

		$this->tpl->assign("memberac_data", $memberac_data);
		$this->tpl->display('memberac/memberac.info.tpl.php');
	}
	/*职位任命*/
	public function actionSeclist(){
		$student_id="m.`student_id`=s.`student_id`";
		$sec_lists=$this->db->fetchAll("SELECT * FROM `tbl_memberac` as m inner join `tbl_student` as s WHERE {$student_id} ");
		$this->tpl->assign("sec_lists", $sec_lists);
		$this->tpl->display('memberac/memberac.seclist.tpl.php');
	}
	/*更改职位*/
	public function actionUpsec(){
		$memberac_id  =(int)$this->get->memberac_id;
		$memberac_data=$this->db->fetchROW("SELECT * FROM `tbl_memberac` WHERE memberac_id={$memberac_id}");
		if ($_POST) {
			$memberac_sec = $this->post->memberac_sec;
			$info =array(
			"memberac_sec" => $memberac_sec,
			);
			$this->db->do_update("tbl_memberac", $info, "memberac_id='{$memberac_id}'");
			// 跳转
			Helper_Client::location("修改成功", "index.php?a=memberac.seclist");
		}


		$this->tpl->assign("memberac_data", $memberac_data);
		$this->tpl->display('memberac/memberac.upsec.tpl.php');
	}
	/*删除职务*/
	public function actionDelsec(){
		$memberac_id = (int)$this->get->memberac_id;
		$mem_data    = $this->db->fetchRow("SELECT * FROM `tbl_memberac` WHERE memberac_id='{$memberac_id}' ");

		if (empty($mem_data)){
			Helper_Client::alert("人员不存在");
		}

		// 修改status = 2
		$info = array(
		"status" => 2,
		"memberac_sec"=>"预备团员",
		);
		$this->db->do_update("tbl_memberac", $info, "memberac_id='{$memberac_id}'");

		// 跳转
		Helper_Client::reurl("index.php?a=memberac.seclist");
	}


	/** 考勤导入 **/
	public function actionCheckIn()
	{
// 当前学期
        $semester_data = $this->model->semester->getNew();
        $semester_id   = $semester_data['semester_id'];
		if ($_POST){
            $attend_smonth = $this->post->attend_smonth;
            // 上传excel
            //$data_lists = $this->model->excel->in("attend");

			// 上传excel
			$data_lists = $this->model->excel->in("check");
            //var_dump($data_lists);exit;
			foreach ($data_lists as $key => $value){
				
				$student_no   = $value[2];
				$student_name = $value[3];
				//echo $student_no;exit;
                $this->db->query("DELETE  FROM `tbl_sttend` WHERE student_no='{$student_no}'
AND  student_name='{$student_name}' AND semester_id='{$semester_id}' AND attend_smonth='{$attend_smonth}' ");
				// 写入数据库

				$info = array(
                    "semester_id"   => $semester_id,
                    "attend_smonth" => $attend_smonth,

				"student_no"    => $value[1],
                    "student_name"    => $value[2],
				"early"   => $value[3],
                "late"    => $value[4],
                "ill"     => $value[5],
                "thing"   => $value[6],
                "esc"     => $value[7],
				"remark"    => $value[8],

				);
                $this->db->do_insert("tbl_sttend", $info);
			}




            // 跳转
			Helper_Client::location("添加成功", "index.php?a=memberac.check");

		}
// 学月列表
        $month_lists = $this->model->semester->getMonthAll($semester_id);

        $this->tpl->assign("semester_data", $semester_data);
        $this->tpl->assign("month_lists", $month_lists);
		$this->tpl->display("memberac/memberac.checkin.tpl.php");


	}

	/** 考勤导出模板 **/
	public function actionCheckOutTp()
	{
		// 模板标题
		$this->model->excel->excel_ini['check']['title'] = "值日生考勤表";


		// 导出模板 - 6行空数据
		$this->model->excel->outTp("check");

		//$this->tpl->display();
//        $check_lists = $this->db->fetchAll("SELECT a.*,b.student_name,b.student_no FROM `tbl_appoint_stu` as a , `tbl_student` as b where a.student_id = b.student_id");
//        foreach ($check_lists as $key => $value){
//            $student_data = $this->model->student->getInfo($value['student_id']);
//
//
//            $info = array(
//                "1" => $student_data['student_no'],
//                "2" => $student_data['student_name'],//ming
//
//            );
//            $data_lists[$key] = $info;
//
//        }
//
//
//        // 模板标题
//        $this->model->excel->excel_ini['check']['title'] = "值日生考勤表";
//        $this->model->excel->outData("check", $data_lists);

	}
	
	/** 考勤导出 **/
	public function actionCheckOutData()
	{

        $semester_id   = $this->get->semester_id?$this->get->semester_id:1;
        $attend_smonth = $this->get->attend_smonth?$this->get->attend_smonth:1;

        $semester_data = $this->model->semester->getInfo($semester_id);
        //var_dump($semester_data);semester_id

        $where = " WHERE semester_id='{$semester_id}' AND attend_smonth='{$attend_smonth}' ";


		$check_lists = $this->db->fetchAll("SELECT * FROM `tbl_sttend` {$where} ");
		foreach ($check_lists as $key => $value){
			//$student_data = $this->model->student->getInfo($value['student_id']);


			$info = array(
			"0" => $value['student_no'],
			"1" => $value['student_name'],
			"2" => $value['early'],
            "3" => $value['late'],
            "4" => $value['ill'],
            "5" => $value['thing'],
            "6" => $value['esc'],//?'是':'否'
			"7" => $value['remark'],
			);
			$data_lists[$key] = $info;
			
		}
	
		
		// 模板标题
		$this->model->excel->excel_ini['check']['title'] = "值日生考勤表";
		$this->model->excel->outData("check", $data_lists);

		$this->tpl->display();



	}	
	

	/*值日生考勤*/
	public function actionCheck(){

        $semester_id   = $this->get->semester_id?$this->get->semester_id:1;
        $attend_smonth = $this->get->attend_smonth?$this->get->attend_smonth:1;

        $semester_data = $this->model->semester->getInfo($semester_id);
        //var_dump($semester_data);semester_id

        $where = " WHERE semester_id='{$semester_id}' AND attend_smonth='{$attend_smonth}' ";

		$student_id="c.`student_id`= s.`student_id`";
		//分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_sttend` {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$check_lists=$this->db->fetchAll("SELECT * FROM `tbl_sttend`  {$where}  LIMIT {$start}, ".PAGE_LIMIT."");
		//var_dump($check_lists);
        $semester_lists = $this->model->semester->getIni();
       // var_dump($semester_lists);
        $month_lists = $this->model->semester->getMonthAll($semester_id);
        //var_dump($month_lists);
        $this->tpl->assign("page", $page->show(4, "yahoo"));
        $this->tpl->assign("semester_id", $semester_id);
        $this->tpl->assign("attend_smonth", $attend_smonth);
        $this->tpl->assign("semester_data", $semester_data);
        $this->tpl->assign("semester_lists", $semester_lists);
        $this->tpl->assign("month_lists", $month_lists);
		$this->tpl->assign("check_lists", $check_lists);
		$this->tpl->display('memberac/memberac.check.tpl.php');
	}
	/*修改值日生考勤*/
	public function actionUpcheck(){
		$check_id  = $this->get->check_id;
		$student_id="c.`student_id`= s.`student_id`";
		$stu_data  = $this->db->fetchRow("SELECT * FROM `tbl_check` AS c inner join `tbl_student` AS s WHERE {$student_id}");
		if ($_POST) {
			$check_id   = $this->post->check_id;
			$check_msg  = $this->post->check_msg;
			$check_date = $this->post->check_msg;
			$check_name = $this->post->check_name;
			$str="";
			foreach ($check_name as $value) {
				$str=$str."\n".$value;

			}
			$info=array(
			"check_name"  => $str,
			"check_date"  => $check_date,
			"check_msg"  => $check_msg,

			);
			$this->db->do_update("tbl_check", $info, "check_id='{$check_id}'");
			Helper_Client::location("修改考勤成功", "index.php?a=memberac.check");

		}
		$this->tpl->assign("stu_data", $stu_data);
		$this->tpl->display('memberac/memberac.upcheck.tpl.php');
	}
	/**导入信息 **/
    public function actionIn()
    {
        $semester_data = $this->model->semester->getNew();
        $semester_id   = $semester_data['semester_id'];

        if($_POST){
            $attend_smonth = $this->post->attend_smonth;
//            // 上传excel
            $data_lists = $this->model->excel->in("check");

            foreach ($data_lists as $key => $value){
                $this->db->query("DELETE FROM `tbl_sttend` WHERE semester_id='{$semester_id}' AND attend_smonth='{$attend_smonth}' AND `student_no` ={$value[1]}");
                $info = array(
                    'semester_id' => $semester_id,
                    'attend_smonth'=> $attend_smonth,
                    'student_no'  => $value[1],
                    'student_name'=>$value[2],
                    'early'        =>$value[3],
                    'late'          =>$value[4],
                    'ill'           =>$value[5],
                    'thing'         =>$value[6],
                    'esc'           =>$value[7],
                    'remark'        =>$value[8],

                );




                $this->db->do_insert("tbl_sttend", $info);


                // 处理后存入数据库

                exit();
            }
		}
        // 学月列表
        $month_lists = $this->model->semester->getMonthAll($semester_id);

        $this->tpl->assign("semester_data", $semester_data);
        $this->tpl->assign("month_lists", $month_lists);

        $this->tpl->display("activitys/activitys.in.tpl.php");
    }

	public function actionReport(){
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_class` ");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$class_lists = $this->db->fetchAll("SELECT * FROM `tbl_class` ORDER by  `class_id` LIMIT {$start}, ".PAGE_LIMIT."");
		$qu_lists  = $this->db->fetchAll("SELECT * FROM `tbl_quantification`ORDER by  `quantification_id`");
		$num_lists = $this->db->fetchAll("SELECT * FROM `tbl_quantification`ORDER by  `quantification_id`");

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("qu_lists", $qu_lists);
		$this->tpl->assign("num_lists", $num_lists);
		$this->tpl->display("memberac/memberac.relist.tpl.php");

	}
	public function actionPlaninfo(){
		$activitys_id = $this->get->activitys_id;
		$act_data = $this->db->fetchRow("SELECT * FROM `tbl_activitys` WHERE activitys_id = {$activitys_id}");
		if (empty($act_data)) {
			Helper_Client::alert('没有这个活动！');
		}
		$this->tpl->assign("act_data", $act_data);
		$this->tpl->display('memberac/memberac.planinfo.tpl.php');
	}

}

?>